package com.bdqn.t350.ch09.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t350.ch09.bean.User;
import com.bdqn.t350.ch09.mapper.UserMapper;
import com.bdqn.t350.ch09.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public List<User> selectList(QueryWrapper queryWrapper) {
        //根据查询的条件首先去redis中获取key为list
        List<User> list = (List<User>) redisTemplate.opsForValue().get("list");
        //如果list等于null,说明redis中没有找到该数据
        if (list == null) {
            //从数据库中获取数据
            list = userMapper.selectList(queryWrapper);
            //将获取到的数据放到redis中，方便下次能过从redis中获取该数据
            redisTemplate.opsForValue().set("list", list);
        }
        return list;
        //return userMapper.selectList(queryWrapper);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public IPage<User> selectPage(IPage<User> page, QueryWrapper queryWrapper) {
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateById(User user) {


        return userMapper.updateById(user);
    }

    public User selectById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public User selectByName(String usrName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Page<User> listPageAssociation(Page<User> page, User user) {
        return userMapper.listPageAssociation(page, user);
    }

    @Override
    public List<String> getByUserNameRightCode(User user) {
        return userMapper.getByUserNameRightCode(user);
    }
}
